package menu.view 
{
	import flash.display.Sprite;
	import flash.ui.Keyboard;
	import interfaces.IOperate;
	import menu.MenuC;
	import morn.core.components.Clip;
	import org.airmvc.Dispatcher;
	import ui.menu.GeneralMenuUI;
	
	/**
	 * ...
	 * @author WLDragon 2014-03-18
	 */
	public class GeneralMenu extends GeneralMenuUI implements IOperate 
	{
		
		public function GeneralMenu() 
		{
			super();
			
			init();
		}
		
		private function init():void 
		{
			itemPanel = new Sprite();
			itemPanel.x = 36;
			itemPanel.y = 60;
			addChild(itemPanel);
			
			itemPool = [];
			for (var i:int = 0; i < 10;i++ )
			{
				var m:GeneralItem = new GeneralItem();
				m.y = 32 * i;
				itemPool.push(m);
			}
			
			endItem = new GeneralItem();
			endItem.update(null);
			
			arrow = new Clip("png.common.clip_arrow", 1, 2);
			arrow.mouseChildren = false;
			arrow.mouseEnabled = false;
			arrow.frame = 1;
			arrow.x = 22;
			arrow.y = -16;
			addChild(arrow);
		}
		
		/* INTERFACE interfaces.IOperate */
		
		public function execute(keyCode:int):void 
		{
			switch (keyCode) 
			{
				case Keyboard.W:
					index--;
				break;
				case Keyboard.S:
					index++;
				break;
				case Keyboard.A:
					if (state == Cnt.STATE_4)
					{
						//减兵
						if (curItem.soliders > 0)
						{
							if (curItem.soliders >= 100)
							{
								reservist += 100;
								curItem.soliders -= 100;
							}
							else
							{
								reservist += curItem.soliders;
								curItem.soliders = 0;
							}
							btReservist.text = reservist.toString();
						}
					}
				break;
				case Keyboard.D:
					if (state == Cnt.STATE_4)
					{
						//加兵
						var need:int = 1000 - curItem.soliders;
						if (need > 0 && reservist > 0)
						{
							if (need > 100)
								need = 100;
								
							if (reservist >= need)
							{
								curItem.soliders += need;
								reservist -= need;
							}
							else
							{
								curItem.soliders += reservist;
								reservist = 0;
							}
							btReservist.text = reservist.toString();
						}
					}
				break;
				case Keyboard.SPACE:
					if (state == Cnt.STATE_1 || state == Cnt.STATE_5)
					{
						Dispatcher.send(MenuC.VERIFY_PARAM, this, curItem.general.id);
					}
					else if (state == Cnt.STATE_2 || state == Cnt.STATE_3)
					{
						selectedMultyGenerals();
					}
					else if (state == Cnt.STATE_4 && curItem.general == null)
					{
						var a:Array = [];
						a.push(reservist);
						for (var i:int = 0,len:int = itemPanel.numChildren - 1; i < len; i++) 
						{
							var m:GeneralItem = itemPanel.getChildAt(i) as GeneralItem;
							a.push(m.soliders);
						}
						Dispatcher.send(MenuC.END_CONSCRIPTION, this,a);
					}
				break;
			}
		}
		
		private function selectedMultyGenerals():void
		{
			if (curItem.general == null)
			{
				var a:Array = [];
				var l:int = itemPanel.numChildren - 1;
				for (var i:int = 0; i < l; i++) 
				{
					var m:GeneralItem = itemPool[i];
					if (m.selected)
						a.push(String(m.general.id));
				}
				if (a.length > 0)
					Dispatcher.send(MenuC.VERIFY_PARAM, this, a);
			}
			else
			{
				curItem.selected = !curItem.selected;
			}
		}
		
		public function update(state:int, gs:Array):void
		{
			Kyb.setOperater(this);
			while (itemPanel.numChildren > 0)
			{
				itemPanel.removeChildAt(0);
			}
			
			var len:int = gs.length;
			this.state = state;
			if (state == Cnt.STATE_4)
			{
				reservist = Gbl.activeCastle.reservist;
				btReservist.text = reservist.toString();
				btReservist.visible = true;
				btReservistTxt.visible = true;
			}
			else
			{
				btReservist.visible = false;
				btReservistTxt.visible = false;
			}
			
			var b:Boolean = (this.state == Cnt.STATE_3 || this.state == Cnt.STATE_4 || this.state == Cnt.STATE_5);
			imgHead1.visible = !b;
			imgHead2.visible = b;
			for (var i:int = 0; i < len; i++) 
			{
				var item:GeneralItem = itemPool[i] as GeneralItem;
				item.update(gs[i],!b);
				itemPanel.addChild(item);
			}
			
			if (this.state != Cnt.STATE_1 && this.state != Cnt.STATE_5)
			{
				endItem.y = 32 * len;
				itemPanel.addChild(endItem);
			}
			
			index = 0;
		}
		
		public function get index():int 
		{
			return _index;
		}
		
		public function set index(value:int):void 
		{
			if (value < 0 || value >= itemPanel.numChildren)
				return;
			
			curItem = itemPanel.getChildAt(value) as GeneralItem;
			arrow.y = curItem.y + 65;
			_index = value;
		}
		
		/**当前的状态1-内政单人 2-移动多人 3-军事多人 4-征兵*/
		private var state:int;
		/**子项显示面板*/
		private var itemPanel:Sprite;
		/**子项池*/
		private var itemPool:Array;
		/**当前项*/
		private var curItem:GeneralItem;
		/**结束项*/
		private var endItem:GeneralItem;
		/**当前项的序号*/
		private var _index:int;
		/**箭头*/
		private var arrow:Clip;
		/**预备兵数*/
		private var reservist:int;

	}

}